package com.aysafety.iotdataplatformclient.geosound.services.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.GeosoundModel;
import com.aysafety.iotdataplatformclient.geosound.dao.GeosoundDao;
import com.aysafety.iotdataplatformclient.geosound.services.GeosoundService;
import com.aysafety.iotdataplatformclient.modelConfigurer.GeosoundModelConfiger;

@Service
public class GeosoundServiceImpl implements GeosoundService {

    @Autowired
    GeosoundModelConfiger geosoundModelConfiger;

    @Autowired
    GeosoundDao geosoundDao;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private GeosoundModel transformData(Map<String, Object> temp, Map<String, String> structure) {
        GeosoundModel geosoundModel = new GeosoundModel();
        for (String key : structure.keySet()) {
            String columnKey = structure.get(key);
            if ("EntId".equals(key)) {
                geosoundModel.setEntId(geosoundModelConfiger.getEntId());
            }
            if ("deviceid".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    geosoundModel.setDeviceId((String) temp.get(columnKey));
                }
            }
            if ("emergoa".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    geosoundModel.setEmergoa(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("poklad".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    geosoundModel.setPoklad(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("datetime".equals(key)) {
                String[] columnTemp = columnKey.split("\\+");
                String dateTimeStr = "";
                for (String value : columnTemp) {
                    if (temp.containsKey(value)) {
                        dateTimeStr += temp.get(value) + " ";
                    }
                }
                dateTimeStr = dateTimeStr.trim();
                geosoundModel.setDatetime(DateUtils.fomatDateTime(dateTimeStr));
            }
            if ("workingface".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    geosoundModel.setWorkingFace((String) temp.get(columnKey));
                }
            }
            if ("pos".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    geosoundModel.setPos((String) temp.get(columnKey));
                }
            }
            if ("probepos".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    geosoundModel.setProbePos((String) temp.get(columnKey));
                }
            }
        }
        return geosoundModel;
    }



    public int readHistroyDataCount(Map<String, Object> conditions) {
        return geosoundDao.readHistroyDataCount(conditions);
    }

    public List<GeosoundModel> readHistroyData(Map<String, Object> conditions, int start, int prePage) {
        List<GeosoundModel> readHistroyDataModel = new ArrayList<GeosoundModel>();
        Map<String, String> structure = geosoundModelConfiger.getDatakey();
        List<Map<String, Object>> readHistroyDatas = geosoundDao.readHistroyData(conditions, start, prePage);
        for (Map<String, Object> readHistroyData : readHistroyDatas) {
            GeosoundModel geosoundModel = this.transformData(readHistroyData, structure);
            readHistroyDataModel.add(geosoundModel);
        }
        return readHistroyDataModel;
    }
}
